home *** CD-ROM | disk | FTP | other *** search
/ Tech Arsenal 1 / Tech Arsenal (Arsenal Computer).ISO / tek-06 / an103x.zip / GRACE.C < prev    next >
C/C++ Source or Header  |  1991-04-09  |  5KB  |  264 lines

  1. /*****************************************************************************
  2. * GRACE.C
  3. *
  4. * 91-02-16 Matt Hagen, Novell, Inc.
  5. *****************************************************************************/
  6.  
  7. #include <nwtypes.h>
  8. #include <nwsemaph.h>
  9. #include <nwipxspx.h>
  10. #include <conio.h>
  11. #include <process.h>
  12. #include <stdio.h>
  13. #include <stdlib.h>
  14. #include <signal.h>
  15. #include <errno.h>
  16.  
  17. typedef struct WorkerStructure
  18. {
  19.     struct WorkerStructure *sLink;
  20.     int threadID;
  21. }WORKER;
  22.  
  23. void SignalRoutine(
  24.     void);
  25.  
  26. void WorkerRoutine(
  27.     void);
  28.  
  29. void UpdateRoutine(
  30.     void);
  31.  
  32. #define CONTINUE 0
  33. #define WAIT 1
  34. #define EXIT 2
  35.  
  36. LONG semaphore;
  37. BYTE threadCount=0;
  38. WORKER *sList=NULL;
  39. int updateThread;
  40. BYTE globalExitFlag=CONTINUE;
  41. BYTE threadInitFlag;
  42.  
  43. /*****************************************************************************
  44. * main
  45. *****************************************************************************/
  46.  
  47. main(
  48.     int argc,
  49.     char *argv[])
  50. {
  51.     int a;
  52.     WORKER *w;
  53.  
  54.     signal(SIGTERM,SignalRoutine);
  55.     threadCount++;
  56.  
  57.     semaphore=OpenLocalSemaphore(0);
  58.     if(semaphore==NULL)
  59.     {
  60.         ConsolePrintf("  Cannot allocate semaphore.\n");
  61.         globalExitFlag=EXIT;
  62.         goto exit0;
  63.     }
  64.  
  65.     for(a=0;a<5;a++)
  66.     {
  67.         threadInitFlag=WAIT;
  68.         if(BeginThreadGroup(WorkerRoutine,NULL,NULL,NULL)==EFAILURE)
  69.         {
  70.             ConsolePrintf("  Cannot create session thread #%d.\n",a+1);
  71.             globalExitFlag=EXIT;
  72.             goto exit1;
  73.         }
  74.  
  75.         while(threadInitFlag==WAIT)
  76.             ThreadSwitch();
  77.  
  78.         if(threadInitFlag!=CONTINUE)
  79.         {
  80.             globalExitFlag=EXIT;
  81.             goto exit1;
  82.         }
  83.     }
  84.  
  85.     threadInitFlag=WAIT;
  86.     if(BeginThreadGroup(UpdateRoutine,NULL,NULL,NULL)==EFAILURE)
  87.     {
  88.         ConsolePrintf("  Cannot create screen thread.\n");
  89.         globalExitFlag=EXIT;
  90.         goto exit1;
  91.     }
  92.  
  93.     while(threadInitFlag==WAIT)
  94.         ThreadSwitch();
  95.  
  96.     if(threadInitFlag!=CONTINUE)
  97.     {
  98.         globalExitFlag=EXIT;
  99.         goto exit1;
  100.     }
  101.  
  102.     while(TRUE)
  103.     {
  104.         WaitOnLocalSemaphore(semaphore);
  105.         if(globalExitFlag!=CONTINUE)
  106.             goto exit2;
  107.  
  108.         /* take worker off sleep list */
  109.         /* give request to worker */
  110.         /* wake up worker */
  111.     }
  112.  
  113. exit2:;
  114.     ResumeThread(updateThread);
  115.  
  116. exit1:;
  117.     w=sList;
  118.     while(w!=NULL)
  119.     {
  120.         ResumeThread(w->threadID);
  121.         w=w->sLink;
  122.     }
  123.  
  124.     while(threadCount>1)
  125.         ThreadSwitch();
  126.  
  127. exit0:;
  128.     CloseLocalSemaphore(semaphore);
  129.  
  130.     threadCount--;
  131. }
  132.  
  133. /*****************************************************************************
  134. * SignalRoutine
  135. *****************************************************************************/
  136.  
  137. void SignalRoutine(
  138.     void)
  139. {
  140.     globalExitFlag=EXIT;
  141.  
  142.     SignalLocalSemaphore(semaphore);
  143.  
  144.     while(threadCount>0)
  145.         ThreadSwitch();
  146. }
  147.  
  148. /*****************************************************************************
  149. * WorkerRoutine
  150. *****************************************************************************/
  151.  
  152. void WorkerRoutine(
  153.     void)
  154. {
  155.     WORKER *w;
  156.     void *ecb;
  157.  
  158.     signal(SIGTERM,SignalRoutine);
  159.     threadCount++;
  160.  
  161.     w=malloc(sizeof(WORKER));
  162.     if(w==NULL)
  163.     {
  164.         ConsolePrintf("  Cannot allocate WORKER structure.\n");
  165.         threadInitFlag=EXIT;
  166.         goto exit0;
  167.     }
  168.  
  169.     ecb=malloc(sizeof(IPX_ECB));
  170.     if(ecb==NULL)
  171.     {
  172.         ConsolePrintf("  Cannot allocate session memory.\n");
  173.         threadInitFlag=EXIT;
  174.         goto exit1;
  175.     }
  176.  
  177.     w->threadID=GetThreadID();
  178.     threadInitFlag=CONTINUE;
  179.  
  180.     while(TRUE)
  181.     {
  182.         w->sLink=sList;
  183.         sList=w;
  184.         SuspendThread(w->threadID);
  185.  
  186.         if(globalExitFlag!=CONTINUE)
  187.             goto exit2;
  188.  
  189.         /* service the request */
  190.     }
  191.  
  192. exit2:;
  193.     free(ecb);
  194.  
  195. exit1:;
  196.     free(w);
  197.  
  198. exit0:;
  199.     threadCount--;
  200.     SuspendThread(GetThreadID());
  201. }
  202.  
  203. /*****************************************************************************
  204. * UpdateRoutine
  205. *****************************************************************************/
  206.  
  207. void UpdateRoutine(
  208.     void)
  209. {
  210.     int screen;
  211.     LONG count=0;
  212.  
  213.     signal(SIGTERM,SignalRoutine);
  214.     threadCount++;
  215.  
  216.     updateThread=GetThreadID();
  217.  
  218.     screen=CreateScreen("Graceful Screen",AUTO_DESTROY_SCREEN);
  219.     if(screen==EFAILURE)
  220.     {
  221.         ConsolePrintf("  Cannot create screen.\n");
  222.         threadInitFlag=EXIT;
  223.         goto exit0;
  224.     }
  225.  
  226.     HideInputCursor();
  227.  
  228.     if(DisplayScreen(screen)!=ESUCCESS)
  229.     {
  230.         ConsolePrintf("  Cannot display screen.\n");
  231.         threadInitFlag=EXIT;
  232.         goto exit1;
  233.     }
  234.  
  235.     threadInitFlag=CONTINUE;
  236.  
  237.     gotoxy(20,0);
  238.     printf("Graceful Init/Deinit Example Screen");
  239.  
  240.     while(TRUE)
  241.     {
  242.         SetCurrentScreen(screen);
  243.  
  244.         gotoxy(0,2);
  245.         printf("Update Count = %u\n",count++);
  246.         printf("Thread Count = %u",threadCount);
  247.         delay(1000);
  248.  
  249.         if(globalExitFlag!=CONTINUE)
  250.             goto exit1;
  251.     }
  252.  
  253. exit1:;
  254.     DestroyScreen(screen);
  255.  
  256. exit0:;
  257.     threadCount--;
  258.  
  259.     SuspendThread(updateThread);
  260. }
  261.  
  262. /****************************************************************************/
  263. /****************************************************************************/
  264.